import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { fetchDetail, fetchComments, fetchAddComments } from '@/api/house';
import { fetchOrderDetail, fetchAddOrder, fetchDeleteOrder } from '@/api/order';
import { PAGE_PARAMS } from '@/emuns';
import { IPageProps, IHouseDetailProps, Dispatch } from '@/interface';

interface CounterState {
  detail: IHouseDetailProps | null;
  comments: any[];
  showLoadMore: boolean;
  reloadCommentsNum: number;
  page: IPageProps;
  order: any;
}

const initialState: CounterState = {
  detail: null,
  comments: [],
  showLoadMore: true,
  reloadCommentsNum: 0,
  page: PAGE_PARAMS,
  order: null,
};

const houseSlice = createSlice({
  name: 'house',
  initialState,
  reducers: {
    restState: (state, action: PayloadAction<{}>) => {
      Object.keys(initialState).forEach(key => {
        if (key !== 'detail') {
          state[key] = initialState[key];
        }
      });
      Object.keys(action.payload).forEach(key => {
        state[key] = action.payload[key];
      });
    },
    changeDetail: (state, action: PayloadAction<IHouseDetailProps>) => {
      state.detail = action.payload;
    },
    changeComments: (state, action: PayloadAction<[]>) => {
      state.comments = [...state.comments, ...action.payload];
    },
    changeLoadMore: (state, action: PayloadAction<boolean>) => {
      state.showLoadMore = action.payload;
    },
    changeReloadCommentsNum: state => {
      state.reloadCommentsNum = state.reloadCommentsNum + 1;

      state.page = {
        ...PAGE_PARAMS,
        pageNum: state.page.pageNum + 1,
      };
    },
    changeOrderDetail: (state, action: PayloadAction<boolean>) => {
      state.order = action.payload;
    },
  },
});

export const {
  changeDetail,
  changeComments,
  changeLoadMore,
  changeReloadCommentsNum,
  restState,
  changeOrderDetail,
} = houseSlice.actions;

export const loadDetail = (params: {}) => {
  return async (dispatch: Dispatch) => {
    const ret = await fetchDetail(params);
    dispatch(changeDetail(ret));
  };
};

export const loadComments = (params: {}) => {
  return async (
    dispatch: Dispatch,
    getState: () => { house: CounterState },
  ) => {
    const { house } = getState();
    const lists = await fetchComments({ ...params, ...house.page });
    dispatch(changeComments(lists));
    dispatch(
      changeLoadMore(Array.isArray(lists) && lists.length ? true : false),
    );
  };
};

export const loadAddComment = (data: any) => {
  return async (
    dispatch: Dispatch,
    getState: () => { house: CounterState },
  ) => {
    const { errorCode } = await fetchAddComments(data);

    if (errorCode === 0) {
      const { house } = getState();
      dispatch(
        restState({
          reloadCommentsNum: house.reloadCommentsNum + 1,
        }),
      );
    }
  };
};

export const loadOrderDetail = (params: any) => {
  return async (dispatch: Dispatch) => {
    const data = await fetchOrderDetail(params);
    dispatch(changeOrderDetail(data));
  };
};

export const loadAddOrder = (params: any) => {
  return async (dispatch: Dispatch) => {
    const { errorCode, data } = await fetchAddOrder(params);
    if (errorCode === 0) {
      dispatch(changeOrderDetail(data));
    }
  };
};
export const loadDeleteOrder = (params: any) => {
  return async (dispatch: Dispatch) => {
    const { errorCode, data } = await fetchDeleteOrder(params);
    if (errorCode === 0) {
      dispatch(changeOrderDetail(data));
    }
  };
};

export default houseSlice.reducer;
